<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Check extends CI_Controller {
	
	function order_check($guid, $type){
		$this->load->model('check_model');
		
		switch ($type) {
			case 1:
				$tmp = $this->check_model->get_order_data($guid);
				$data = $tmp[0];
				$data['type'] = $type;
				$data['guid'] = $guid;
				//exit (print_r($data));
				$this->load->view('head_light');
				$this->load->view('check_form', $data);
				$this->load->view('end');
				break;
			
			case 2:
				$tmp = $this->check_model->get_order_data($guid);
				$data = $tmp[0];
				$data['type'] = $type;
				$data['guid'] = $guid;
				//exit (print_r($data));
				$this->load->view('head_light');
				$this->load->view('check_form', $data);
				$this->load->view('end');				
				break;

			case 3:
				$tmp = $this->check_model->get_order_data($guid);
				$data = $tmp[0];
				$data['type'] = $type;
				$data['guid'] = $guid;
				//exit (print_r($data));
				$this->load->view('head_light');
				$this->load->view('check_form', $data);
				$this->load->view('end');					
				break;
		}

	}
	function send_mail($email, $guid, $type){
		$this->load->library('email');
		$this->email->from('hotab.jn@gmail.com');
		$this->email->to($email);
		$this->email->subject('New order');
		$this->email->message('Был зарегестрирован заказ, проверьте данные в прикрепленном файле и перейдите по следующей ссылке для подтверждения правильности http://price_ci/check/order_check/'.$guid.'/'.$type);
		$this->email->attach('orders/'.$guid.'.pdf');
		$this->email->send();
		//echo $this->email->print_debugger();
	}
	function send_last_mail($email){
		$this->load->library('email');
		$this->email->from('hotab.jn@gmail.com');
		$this->email->to($email);
		$this->email->subject('Last confirm');
		$this->email->message('Заказ был принят и успешно обработан! Congratulations!!!');
		$this->email->send();
		//echo $this->email->print_debugger();
	}
	
	function save_changes(){
		$data['guid'] = $this->input->post('guid');
		$data['type'] = $this->input->post('change_type');
		$this->load->model('check_model');
		$this->load->helper('url');
		switch ($data['type']){
			case 1:
				$data['column'] = 'notSpam';
				$this->check_model->update_order_data($data);
				$email = $this->check_model->get_pr_email($data['guid']);
				//exit ($email);
				$this->send_mail($email, $data['guid'], 2);
				break;	
			case 2:
				$data['column'] = 'checkOne';
				$this->check_model->update_order_data($data);
				$email = $this->check_model->get_oto_email($data['guid']);
				//exit ($email);
				$this->send_mail($email, $data['guid'], 3);
				break;		
			case 3:
				$data['column'] = 'checkTwo';
				$this->check_model->update_order_data($data);
				$email[] = $this->check_model->get_pr_email($data['guid']);
				$email[] = $this->check_model->get_client_email($data['guid']);
				$this->send_last_email($email);
				break;	
		}
		redirect('/admin/', 'refresh');
	}
}
?>